package avayajtapirecepcion;

import javax.telephony.JtapiPeerFactory;
import javax.telephony.JtapiPeerUnavailableException;
import javax.telephony.ProviderEvent;
import javax.telephony.ProviderListener;

public class ListenerServer implements ProviderListener {

	Recepcion recepcion;
	public ListenerServer(Recepcion r)
	{
		this.recepcion=r;
	}
	
	@Override
	public void providerEventTransmissionEnded(
			ProviderEvent arg0) {
		//System.out.println("finalizado");

			}

	@Override
	public void providerInService(ProviderEvent arg0) {
		System.out.println("En servicio");
		
	}

	@Override
	public void providerOutOfService(ProviderEvent arg0) {
		System.out.println("fuera de servicio");
		int a=0;
		while(a==0)
		{
			System.out.println("reconectando...");
			try
			{
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
					
				}
				try {	
					recepcion.peer = JtapiPeerFactory.getJtapiPeer(null);
				} catch (JtapiPeerUnavailableException e) {
				}
	
				recepcion.services = recepcion.peer.getServices();
				if (recepcion.services.length == 0) {
					continue;
				}
				recepcion.conexion();
				recepcion.asociarExtension();
				a=1;
			}
			catch(Excepciones e)
			{
				a=0;
				System.out.println(e.getMessage());
			}
		}
		System.out.println("reestableciendo conexion");
	}

	@Override
	public void providerShutdown(ProviderEvent arg0) {
		System.out.println("servidor apagado");
		int a=0;
		while(a==0)
		{
			System.out.println("reconectando...");
			try
			{
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
					
				}
				try {	
					recepcion.peer = JtapiPeerFactory.getJtapiPeer(null);
				} catch (JtapiPeerUnavailableException e) {
				}
	
				recepcion.services = recepcion.peer.getServices();
				if (recepcion.services.length == 0) {
					continue;
				}
				recepcion.conexion();
				recepcion.asociarExtension();
				a=1;
			}
			catch(Excepciones e)
			{
				a=0;
				System.out.println(e.getMessage());
			}
		}
		System.out.println("reestableciendo conexion");
	}


}
